package com.suning.sawp.service.util;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.atomic.AtomicInteger;

/**
 * 线程池管理工具类
 * @author 14043094
 * @see [相关类/方法]（可选）
 * @since [产品/模块版本] （可选）
 */
public class SingleThreadFactory implements ThreadFactory {

    static final AtomicInteger POOL_NUMBER = new AtomicInteger(1);
    final ThreadGroup group;
    final AtomicInteger threadNumber = new AtomicInteger(1);
    final String namePrefix;
    /**
     * 新建一个线程池
     */
    private static ExecutorService executorService = Executors.newCachedThreadPool(new SingleThreadFactory(
            "-async-thread-"));
    /**
     * 
     * 功能描述: <br>
     * 〈功能详细描述〉
     *
     * @return
     * @see [相关类/方法](可选)
     * @since [产品/模块版本](可选)
     */
    public static ExecutorService getExecutorService() {
        return executorService;
    }

    /**
     * 
     * @param threadName
     */
    public SingleThreadFactory(String threadName) {
        SecurityManager s = System.getSecurityManager();
        group = (s != null) ? s.getThreadGroup() : Thread.currentThread().getThreadGroup();
        namePrefix = "pool-" + POOL_NUMBER.getAndIncrement() + threadName;
    }

    @Override
    public Thread newThread(Runnable r) {
        Thread t = new Thread(group, r, namePrefix + threadNumber.getAndIncrement(), 0);
        t.setDaemon(true);
        if (t.getPriority() != Thread.MIN_PRIORITY) {
            t.setPriority(Thread.MIN_PRIORITY);
        }
        return t;
    }
}
